package com.jp.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.jp.Main;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogUtils {

    public static String getFilePath() throws Exception {
        String jarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
        return URLDecoder.decode(jarPath, "UTF-8");
    }

    /**
     * 获得对应的时间一级message
     */
    public static Map<String, String> getMsgInfo(String msg) {
        HashMap<String, String> map = null;

        Pattern compileOfDate = Pattern.compile("[0-9]{10}[ ][+][0-9]{4}");

        int i = msg.indexOf(':');
        Matcher m1 = compileOfDate.matcher(msg);

        // 判断是否找到对应的字符，并取出
        if (m1.find() && i != -1) {
            map = new HashMap<>(1);
            map.put(getDate(m1.group(0)), msg.substring(i + 2));
        }

        return map;
    }

    /**
     * 根据秒数获得日期时间
     */
    private static String getDate(String date) {
        long dateOfMs = Long.parseLong(date.substring(0, 11).trim()) * 1000;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return simpleDateFormat.format(new Date(dateOfMs));
    }

    /**
     * 根据日期进行分组
     */
    public static Map<String, List<String>> getInfoByGroup(List<Map<String, String>> list) {
        HashMap<String, List<String>> map = new HashMap<>();

        for (Map<String, String> item : list) {
            for (Map.Entry<String, String> entry : item.entrySet()) {
                String key = entry.getKey().substring(0, 10);

                if (map.containsKey(key)) {
                    map.get(key).add(entry.getValue());
                } else {
                    List<String> resultMapValueList = new ArrayList<>();
                    resultMapValueList.add(entry.getValue());
                    map.put(key, resultMapValueList);
                }
            }
        }

        HashMap<String, List<String>> resultMap = new LinkedHashMap<>();

        map.keySet().stream().sorted(Comparator.reverseOrder()).forEach(m -> resultMap.put(m, map.get(m)));

        return resultMap;
    }

    public static List<String> getDates(String model) {
        List<String> resultList = new ArrayList<>();
        Integer type = Main.map.get(model);

        BiFunction<Date, Date, List<String>> getDateList = (startDate, endDate) -> {
            ArrayList<String> list = new ArrayList<>();

            long count = DateUtil.betweenDay(startDate, endDate, true);

            String format = DateUtil.format(startDate, Main.DATE_FORMAT);
            list.add(format);

            for (int i = 1; i < count + 1; i++) {
                DateTime dateTime = DateUtil.offsetDay(startDate, i);
                list.add(DateUtil.format(dateTime, Main.DATE_FORMAT));
            }

            return list;
        };

        switch (type) {
            case 0:
                resultList.add(DateUtil.now().substring(0, 10));
                break;
            case 1:
                resultList.add(DateUtil.yesterday().toString().substring(0, 10));
                break;
            case 2:
                resultList.addAll(getDateList.apply(DateUtil.beginOfWeek(new Date()), new Date()));
                break;
            case 3:
                resultList.addAll(getDateList.apply(DateUtil.beginOfWeek(DateUtil.lastWeek()), DateUtil.endOfWeek(DateUtil.lastWeek())));
                break;
            case 4:
                resultList.addAll(getDateList.apply(DateUtil.beginOfMonth(new Date()), new Date()));
                break;
            case 5:
                resultList.addAll(getDateList.apply(DateUtil.beginOfMonth(DateUtil.lastMonth()), DateUtil.endOfMonth(DateUtil.lastMonth())));
                break;
            default:
                break;
        }

        return resultList;
    }
}
